<?php

/**
 * @author ThanhKhoaIT
 * @copyright 2013
 */
 
class Owl
{
    function Connect()
    {
        $hosting  = "localhost";
        $username = "root";
        $password = "";
        $database = "databasename";
        
        $con = mysqli_connect($hosting, $username, $password, $database);
        if (!$con)
        {
            die('Could not connect: ' . mysqli_error());
        }
        $con->set_charset("UTF8");

        return $con;
    }
}
    
class DB extends Owl
{
    function update($table, $data, $where)
    {
        $update = '';
        
        $sql = "SELECT * FROM $table LIMIT 0";
        $OwlMeta = mysqli_query(Owl::Connect(), $sql);
        
        $i = 0;
        while($i < mysqli_num_fields($OwlMeta))
        {
            $meta = mysqli_fetch_field($OwlMeta);  
            if(isset($data[$meta->name]))
            {
                $update = $update."$meta->name = N'{$data[$meta->name]}',";
            }
            $i++;
        }
        
        $update = substr($update,0,strlen($update) - 1);
        
        $sql =  "UPDATE $table SET $update ".$where;
        
        if (mysqli_query(Owl::Connect(), $sql))
        {
            return 1;
        }
        return -1;
    }
    
    function insert($table, $data)
    {
        $field = '';
        $insert = '';
        
        $sql = "SELECT * FROM $table LIMIT 0";
        $OwlMeta = mysqli_query(Owl::Connect(), $sql);

        $i = 0;
        while($i < mysqli_num_fields($OwlMeta))
        {
            $meta = mysqli_fetch_field($OwlMeta);  
            if(isset($data[$meta->name]))
            {
                $field = $field."$meta->name,";
                $insert = $insert."N'{$data[$meta->name]}',";
            }
            $i++;
        }
        
        $field = substr($field,0,strlen($field) - 1);
        $insert = substr($insert,0,strlen($insert) - 1);
        
        $sql =  "INSERT INTO $table ($field) VALUES ($insert) ";

        if (!mysqli_query(Owl::Connect(), $sql))
        {
            return -1;
        }
        return 1;
    }
    
    function select($table, $data, $where, $order)
    {
        $field = '';
        
        $sql = "SELECT * FROM $table LIMIT 0";
        $OwlMeta = mysqli_query(Owl::Connect(), $sql);
        $i = 0;
        while($i < mysqli_num_fields($OwlMeta))
        {
            $meta=mysqli_fetch_field($OwlMeta);  
            if(isset($data[$meta->name]))
            {
                $field = $field."$meta->name,";
            }
            $i++;
        }
        $field = substr($field,0,strlen($field) - 1);
        $sql = "SELECT $field FROM $table ".$where .$order;
        return mysqli_query(Owl::Connect(), $sql);
    }
    
    function count($table, $where)
    {
        $field = '';
        
        $sql = "SELECT count(*) AS KQ FROM $table $where";
        $resual = mysqli_query(Owl::Connect(), $sql);
        $row = mysqli_fetch_array($resual);
        return $row['KQ'];
    }
}
?>